package com.heima.wemedia.feign;

import com.heima.common.constants.WemediaConstants;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.comment.dtos.ScanCommentContentDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.wemedia.service.GreenScanService;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsAutoScanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class WemediaClient implements IWemediaClient {

    @Autowired
    private WmChannelService wmChannelService;
    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;
    @Autowired
    private GreenScanService greenScanService;

    @GetMapping("/api/v1/channel/list")
    @Override
    public ResponseResult getChannels() {
        return ResponseResult.okResult(wmChannelService.list());
    }

    /**
     * 进行评论内容的审核
     * @param contentDto
     * @return
     */
    @PostMapping("/api/v1/comment/scan")
    @Override
    public Boolean commentScan(@RequestBody ScanCommentContentDto contentDto){

        //标记位，默认为审核通过
        boolean flag=true;

        //1.参数校验
        if(StringUtils.isBlank(contentDto.getContent())){
            return true;
        }
        //2.先进行自管理敏感词的审核
        flag = wmNewsAutoScanService.handleSensitiveScan(contentDto.getContent(), null);
        if(!flag){
            return flag;
        }
        //3.自审核通过，再进行百度审核
        try {
            //调用百度云API进行内容审核
            Map map = greenScanService.scanText(contentDto.getContent());
            //不通过：修改文章状态
            if ((int) map.get("conclusionType") == WemediaConstants.BAIDU_SCAN_NO_PASS) {
                flag = false;
            }
            //疑似：修改文章状态
            if ((int) map.get("conclusionType") == WemediaConstants.BAIDU_SCAN_YS) {
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //4.全部通过返回ture
        return flag;
    }
}